//
//  YSGMyopiaHospitalListController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/6.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class YSGMyopiaHospitalListController: BaseViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var selectedClosure: ((_ model: MyopiaHospitalModel) -> Void)?
    
    var viewModel: MyopiaHospitalViewModel? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "选择检查医院"
        setUI()
        beginRefresh()
        // Do any additional setup after loading the view.
    }

    // MARK: - UI
    func setUI() {
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.mj_header = refreshHeader(action: #selector(loadData))
        tableView.register(YSGMyopiaHospitalListCell.nibForCell(),
                           forCellReuseIdentifier: YSGMyopiaHospitalListCell.reuseIdentifier())
    }

}

extension YSGMyopiaHospitalListController: UITableViewDataSource, UITableViewDelegate {
    // MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YSGMyopiaHospitalListCell.reuseIdentifier(), for: indexPath) as! YSGMyopiaHospitalListCell
        if let vm = viewModel  {
            let model = vm.hospitalModel(indexPath: indexPath)
            cell.updateCellWithModel(model: model)
        }
        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel?.numberOfSection ?? 0
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let vm = viewModel else { return }
        let model = vm.models[indexPath.section]
        selectedClosure?(model)
        navigationController?.popViewController(animated: true)
    }
}

// MARK: - 请求数据
extension YSGMyopiaHospitalListController {
    func beginRefresh() {
        tableView.mj_header?.beginRefreshing()
    }
    
    func endRefresh() {
        tableView.mj_header?.endRefreshing()
    }
    
    @objc func loadData() {
        getHospitalList()
    }
    
    func getHospitalList() {
        MyopiaHospitalViewModel.getMyopiaHospitalList(hosDeviceId: "", hosName: "") { (models, error, code) in
            self.endRefresh()
            switch code {
            case .failure:
                HUDTool.showError(message: error)
            case .success:
                self.viewModel = MyopiaHospitalViewModel(models: models)
            }
        }
    }
}
